define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'roleright/index' + location.search,
                    add_url: 'right/add',
                    edit_url: 'roleright/edit',
                    del_url: 'roleright/del',
                    multi_url: 'roleright/multi',
                    import_url: 'roleright/import',
                    table: 'roleright',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                operate:false,
                showToggle: false,
                showColumns: false,
                visible: false,
                commonSearch: false,
                columns: [
                    [
                        // {checkbox: false},
                        {field: 'id', title: __('Id')},
                        {field: 'tno_code', title: __('Tno_code'), operate: 'LIKE', table: table, class: 'autocontent', formatter: function(value,row,index){

                            if(containsNumber(value))
                            {

                                if(containsCharacter(row.ten_code,'SN'))
                                {
                                    return '<button value="SN'+value+'" class="btn btn-success copy-link" title="Copy" style="font-size: 15px;">'+value+'</button>';
                                }
                                else
                                {
                                    return value;
                                }
                                // return '<button value="'+value+'" class="btn btn-success copy-link" title="Copy" style="font-size: 15px;">'+value+'</button>';
                                
                            }
                            else
                            {
                                return value;
                            }

                            
                        }
                    
                    
                    
                    },
                        {field: 'tno_codename', title: __('Tno_codename'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'ten_code', title: __('Ten_code'), operate: 'LIKE', table: table, class: 'autocontent', formatter:function(value,row,index){

                            if(containsNumber(value)&&containsCharacter(value,'SN'))
                            {
                                return '<button value="'+value+'" class="btn btn-success copy-link" title="Copy" style="font-size: 15px;">'+value+'</button>';
                            }
                            else
                            {
                                if(containsCharacter(value,'SN'))
                                {
                                    return '<span class="btn-success">'+value+'</span>';
                                }
                                else
                                {
                                    return value;
                                }
                                
                            }
                            
                        }},
                        {field: 'ono_code', title: __('Ono_code'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'ono_codename', title: __('Ono_codename'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'oen_code', title: __('Oen_code'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });




            $(document).on("click", ".copy-link", function(){
                var url = $(this).val();
                $(this).removeClass("btn-success").addClass("btn-warning");
                /**点击按钮,复制链接地址...*/
                var oInput = document.createElement('input');
                oInput.value = url;
                document.body.appendChild(oInput);
                oInput.select();//选择对象
                document.execCommand("Copy"); //
                oInput.className = 'oInput'
                oInput.style.display='none';
                layer.msg('复制成功')
               
                })

                function containsNumber(str) {
                    const regex = /\d/;
                    return regex.test(str);
                  }

                  function containsCharacter(str, character) {
                    return str.indexOf(character) !== -1;
                  }



            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
